package com.example.wanglong.myoneapplication.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wanglong.myoneapplication.R;
import com.example.wanglong.myoneapplication.adbanner.VisaImageCycleView;
import com.example.wanglong.myoneapplication.utils.CommonUtil;
import com.example.wanglong.myoneapplication.viewmodel.AdInfo;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.util.ArrayList;

@SuppressLint("NewApi")
public class MainTab01 extends Fragment implements View.OnClickListener
{
	private View mOneView;
	private LinearLayout v;
	private ScrollView mScrollView;
	private RelativeLayout usa;
	private RelativeLayout korea;
	private RelativeLayout japan;
	private RelativeLayout taiwan;
	private RelativeLayout thailand;
	private RelativeLayout singapore;
	private RelativeLayout australia;
	private RelativeLayout english;
	private RelativeLayout canada;

	VisaImageCycleView imageCycleView;

	FragmentMainActivity fragmentMainActivity;

	@Override
	public void onAttach(Context context) {
		fragmentMainActivity=(FragmentMainActivity) getActivity();

		super.onAttach(context);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		mOneView=inflater.inflate(com.example.wanglong.myoneapplication.R.layout.main_tab_01, container, false);

		initData(mOneView);

		return mOneView;
	}



	public void initData(View mOneView) {


		//初始化
		initIndex(mOneView);
		initAD(mOneView, null);

	}

	//初始化
	public void initIndex(View mOneView){

		v =(LinearLayout) mOneView.findViewById(R.id.visatitle);
		v.getBackground().setAlpha(0);

		usa=(RelativeLayout) mOneView.findViewById(R.id.visathree);
		korea=(RelativeLayout) mOneView.findViewById(R.id.visaone);
		japan=(RelativeLayout) mOneView.findViewById(R.id.visatwo);
		taiwan=(RelativeLayout) mOneView.findViewById(R.id.visafour);
		thailand=(RelativeLayout) mOneView.findViewById(R.id.visafive);
		singapore=(RelativeLayout) mOneView.findViewById(R.id.visasix);
		australia=(RelativeLayout) mOneView.findViewById(R.id.visaseven);
		english=(RelativeLayout) mOneView.findViewById(R.id.visaeight);
		canada=(RelativeLayout) mOneView.findViewById(R.id.visanine);

		//滑动title透明度变化
		initChangeTitle();


		usa.setOnClickListener(this);
		korea.setOnClickListener(this);
		japan.setOnClickListener(this);
		taiwan.setOnClickListener(this);
		thailand.setOnClickListener(this);
		singapore.setOnClickListener(this);
		australia.setOnClickListener(this);
		english.setOnClickListener(this);
		canada.setOnClickListener(this);



	}


	@Override
	public void onClick(View v){
		switch (v.getId()){
			case R.id.visathree:
				actionStart(getActivity(),"美国");
				break;
			case R.id.visaone:
				actionStart(getActivity(),"韩国");
				break;
			case R.id.visatwo:
				actionStart(getActivity(),"日本");
				break;
			case R.id.visafour:
				actionStart(getActivity(),"台湾");
				break;
			case R.id.visafive:
				actionStart(getActivity(),"泰国");
				break;
			case R.id.visasix:
				actionStart(getActivity(),"新加坡");
				break;
			case R.id.visaseven:
				actionStart(getActivity(),"澳大利亚");
				break;
			case R.id.visaeight:
				actionStart(getActivity(),"英国");
				break;
			case R.id.visanine:
				actionStart(getActivity(),"加拿大");
				break;
		}
	}




	public static void actionStart(Context context,String city){
		Intent intent=new Intent(context,ListActivity.class);
		intent.putExtra("city", city);
		context.startActivity(intent);
	}

	//滑动title透明度变化
	public void initChangeTitle(){
		mScrollView=(ScrollView) mOneView.findViewById(R.id.scrollvisacontent);
		mScrollView.setOnTouchListener(new TouchListenerImpl());
		final ViewTreeObserver.OnScrollChangedListener listener = new ViewTreeObserver.OnScrollChangedListener() {
			@Override
			public void onScrollChanged() {
				System.out.println(mScrollView.getScrollY());
				int sy=mScrollView.getScrollY();
				if(sy>=0 && sy<=255){
					v.getBackground().setAlpha(sy);
				} else if(sy>255){
					v.getBackground().setAlpha(255);
				}
			}
		};
		ViewTreeObserver observer = mScrollView.getViewTreeObserver();
		observer.addOnScrollChangedListener(listener);
	}

	//监控touch事件title透明度变化
	private class TouchListenerImpl implements View.OnTouchListener {
		@Override
		public boolean onTouch(View view, MotionEvent motionEvent) {
			//System.out.println(view.getScrollY());
			int sy=view.getScrollY();
			if(sy>=0 && sy<=255){
				v.getBackground().setAlpha(view.getScrollY());
			} else if(sy>255){
				v.getBackground().setAlpha(255);
			}
			return false;
		}

	}

	//初始化首页广告
	public void initAD(View mOneView,Context context) {

		// 初始化ImageLoader
		@SuppressWarnings("deprecation")
		DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.dsbb) // 设置图片下载期间显示的图片
				.showImageForEmptyUri(R.drawable.dsbb) // 设置图片Uri为空或是错误的时候显示的图片
				.showImageOnFail(R.drawable.dsbb) // 设置图片加载或解码过程中发生错误显示的图片
				.cacheInMemory(true) // 设置下载的图片是否缓存在内存中
				.cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
				.build(); // 创建配置过得DisplayImageOption对象

		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity()).defaultDisplayImageOptions(options)
				.threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).build();
		ImageLoader.getInstance().init(config);


		imageCycleView = (VisaImageCycleView) mOneView.findViewById(R.id.indexCycleView);
		AdInfo adInfo = new AdInfo();
		ArrayList<String> imageUrlList = new ArrayList<String>();
		ArrayList<String> clickUrlList = new ArrayList<String>();
		ArrayList<String> advertisementPosition = new ArrayList<String>();
		//简析报文
		for (int i = 0; i < 3; i++) {
			switch (i){
				case 0:
					imageUrlList.add("drawable://"+R.drawable.yinghua750_360);
					clickUrlList.add("url"+i);
					advertisementPosition.add(String.valueOf(i));
					break;
				case 1:
					imageUrlList.add("drawable://"+R.drawable.online_visa750_360);
					clickUrlList.add("url"+i);
					advertisementPosition.add(String.valueOf(i));
					break;
				case 2:
					imageUrlList.add("drawable://"+R.drawable.taiwan750_360);
					clickUrlList.add("url"+i);
					advertisementPosition.add(String.valueOf(i));
					break;
			}
		}
		adInfo.ClickUrlList = clickUrlList;
		adInfo.ImageUrlList = imageUrlList;
		adInfo.AdvertisementPositionList = advertisementPosition;

		initCarsuelView(adInfo.ClickUrlList, adInfo.ImageUrlList);


	}

	//初始化轮播图
	public void initCarsuelView(final ArrayList<String> imageDescList, ArrayList<String> urlList) {

		LinearLayout.LayoutParams cParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, CommonUtil.getScreenWidth(getActivity()) * 35 / 65);
		imageCycleView.setLayoutParams(cParams);



		VisaImageCycleView.ImageCycleViewListener mAdCycleViewListener = new VisaImageCycleView.ImageCycleViewListener() {
			@Override
			public void onImageClick(int position, View imageView) {
				/**实现点击事件*/
				//String url = imageDescList.get(position);
				actionCycleStart(getActivity(),"home",String.valueOf(position));

			}

			@Override
			public void displayImage(String imageURL, ImageView imageView) {
				ImageLoader.getInstance().displayImage(imageURL, imageView);
			}
		};
		/**设置数据*/
		imageCycleView.setImageResources(imageDescList, urlList, mAdCycleViewListener);
		imageCycleView.startImageCycle();
	}


	public static void actionCycleStart(Context context,String pagesource,String type){
		Intent intent=new Intent(context,WebViewPage.class);
		intent.putExtra("source", pagesource);
		intent.putExtra("type", type);
		context.startActivity(intent);
	}





}
